import 'package:auto_orientation/auto_orientation.dart';
import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/dimens.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/app/theme/text_theme/default_text_theme.dart';
import 'package:bxultimate/data/request/shop_request.dart';
import 'package:bxultimate/data/request/user_request.dart';
import 'package:bxultimate/data/store/auth_store.dart';
import 'package:bxultimate/domain/shop/Shop.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/mk_card_container.dart';
import 'package:bxultimate/widgets/mk_list_cell_item.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'controller.dart';

class UserIndexPage extends StatefulWidget {
  @override
  State<UserIndexPage> createState() => _UserIndexPageState();
}

class _UserIndexPageState extends State<UserIndexPage> {
  final controller = Get.find<UserIndexController>();
  final state = Get.find<UserIndexController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
        child: Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              centerTitle: false,
              backgroundColor: Colors.transparent,
              actions: [
                IconButton(
                    onPressed: controller.scan,
                    icon: MkLoadImage(
                      Assets.userScan,
                      width: 30,
                      height: 30,
                    ))
              ],
            ),
            body: ListView(
              children: <Widget>[
                _buildUserInfo(),
                _buildNav(),
              ],
            )));
  }

  _buildUserInfo() {
    return Container(
      padding: EdgeInsets.only(
          left: AppTheme.iDefaultPadding,
          top: AppTheme.iDefaultPadding,
          bottom: AppTheme.iDefaultPadding),
      // decoration: BoxDecoration(color: Get.theme.cardColor),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Expanded(
              child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Obx(() => Container(
                    decoration: BoxDecoration(
                        border: Border.all(color: Colors.white, width: 3),
                        borderRadius: BorderRadius.all(Radius.circular(34))),
                    child: CircleAvatar(
                      radius: 34,
                      backgroundImage:
                          NetworkImage(AuthStore.to.getUserAvatar()),
                      foregroundColor: Colors.white,
                    ),
                  )),
              Gaps.hGap16,
              InkWell(
                onTap: controller.toUserInformation,
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Obx(() => Text(
                        AuthStore.to.isLogin
                            ? AuthStore.to.profile.nickname!
                            : '请登录',
                        style: defaultTextTheme.titleLarge)),
                    Gaps.vGap5,
                    Row(
                      children: [
                        // _buildIdentityTag(),
                      ],
                    )
                  ],
                ),
              )
            ],
          )),
          GestureDetector(
            onTap: controller.toUserInformation,
            child: Container(
              padding: EdgeInsets.only(
                left: 10,
                top: 5,
                right: 10,
                bottom: 5,
              ),
              decoration: BoxDecoration(
                  color: AppTheme.getCurrentTheme().scaffoldBackgroundColor,
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(40),
                    bottomLeft: Radius.circular(40),
                  )),
              child: Text(
                "个人资料",
                style: defaultTextTheme.bodySmall,
              ),
            ),
          )
        ],
      ),
    );
  }

  _buildNav() {
    return MkCardContainer(
      padding: EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding / 2),
      margin: EdgeInsets.only(
          top: 0,
          left: AppTheme.iDefaultPadding,
          right: AppTheme.iDefaultPadding),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          MkListCellItem(
            textIcon: MkLoadImage(
                "https://minio.sxmbxt.com/static/img/my/order.png",
                width: 18),
            value: '我的订单',
            press: () {
              Get.toNamed(RouterName.userMyOrderList);
            },
          ),
          // MkListCellItem(
          //   textIcon: MkLoadImage(
          //     "https://minio.sxmbxt.com/static/img/my/shop.png",
          //     width: 18,
          //   ),
          //   value: '拥军入住',
          //   press: () async {
          //     // 判断用户是否有商家
          //     Shop? shop = await ShopRequest.checkMyShop();
          //     if (shop != null && shop.id != null) {
          //       Get.toNamed(RouterName.userMerchantEnterDashboard);
          //     } else {
          //       // 创建商家
          //       Get.toNamed(RouterName.userMerchantEnterInformation);
          //     }
          //   },
          // ),
          MkListCellItem(
            textIcon: LoadAssetImage(Assets.userUserIdcard, width: 18),
            value: '电子身份证',
            press: () {
              Get.toNamed(RouterName.electronIdcard);
            },
          ),
          // MkListCellItem(
          //   textIcon: LoadAssetImage(Assets.userUserIdcard, width: 18),
          //   value: '授权申请',
          //   press: () {
          //     Get.toNamed(RouterName.electronIdcard);
          //   },
          // ),
          MkListCellItem(
            textIcon: LoadAssetImage(Assets.userUserSetting, width: 18),
            value: '系统设置',
            press: controller.toSystemSetting,
          ),
        ],
      ),
    );
  }

  _buildIdentityTag() {
    return Container(
      margin: EdgeInsets.only(right: AppTheme.iDefaultPadding / 2),
      padding: EdgeInsets.symmetric(
          vertical: AppTheme.iDefaultPadding / 4.4, horizontal: 10),
      decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
            colors: [
              Color(0xFF73B6FE),
              Color(0xFF3F88F6),
            ],
          ),
          borderRadius: BorderRadius.all(Radius.circular(12))),
      child: Row(
        children: [
          LoadAssetImage(
            Assets.userUserMb,
            width: 12,
          ),
          Gaps.hGap5,
          Text('民兵',
              style:
                  TextStyle(fontSize: Dimens.font_sp12, color: Colors.white)),
        ],
      ),
    );
  }
}
